<?php

namespace Admin\Controller;

class PositionController extends LoginController {
	public function __construct() {
		parent::__construct ();
		$this->db = D ( 'Position' );
		layout ( false );
	}
	public function index() {
		if (IS_POST) {
			$this->checkToken($_POST);
			$arr = $_POST;
			$id = $arr ['id'];
			if (empty ( $id )) {
				$this->db->add ( $arr );
			} else {
				$this->db->save ( $arr );
			}
		}
		$count = $this->db->count (); // 查询满足要求的总记录数 $map表示查询条件
		$Page = $this->getPage ( $count );
		$show = $Page->show ();
		$list = $this->db->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		$this->assign ( 'list', $list );
		$this->assign ( 'show', $show );
		$this->display ();
	}
	public function lists() {
		$this->db = D ( 'position_data' );
		$p_id = $_GET ['p_id'];
		$count = $this->db->where ( "p_id=$p_id" )->count (); // 查询满足要求的总记录数 $map表示查询条件
		$Page = $this->getPage ( $count );
		$show = $Page->show ();
		$list = $this->db->where ( "p_id=$p_id" )->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		$this->assign ( 'list', $list );
		$this->assign ( 'show', $show );
		$this->display ();
	}
	public function positionAdd() {
		if (IS_POST) {
			$this->checkToken($_POST);
			$arr = $_POST;
			$data = array ();
			$name = $arr ['adver'];
			$src = $arr ['adver_src'];
			$url = $arr ['adver_url'];
			foreach ( $name as $key => $r ) {
				$data [$key] ['name'] = $name [$key] ['name'];
				$data [$key] ['url'] = $url [$key] ['url'];
				$data [$key] ['img'] = $src [$key] ['src'];
			}
			$arr ['data'] = $data;
			if ($arr ['id']) {
				$result = $this->db->relation ( true )->delete ( $arr ['id'] );
			}
			$this->db->relation ( true )->add ( $arr );
			
			redirect ( '/admin/position' );
		} else {
			if (IS_GET && ! empty ( $_GET ['id'] )) {
				$id = $_GET ['id'];
				$data = $this->db->relation ( 'data' )->find ( $id );
				$this->assign ( 'data', $data );
			}
			$this->display ( 'adverAdd' );
		}
	}
	
	// 删除广告
	public function positionDe() {
		if (IS_POST) {
			$this->db->relation ( true )->delete ( $_POST ['id'] );
			echo true;
		}
	}
	// 获取推荐位信息
	public function positionUp() {
		if (IS_POST) {
			$data = $this->db->find ( $_POST ['id'] );
			$this->ajaxReturn ( $data );
		}
	}
}